<?php 
namespace app\Home\controller;
use think\Controller;
use think\Db;
use think\Cookie;
use think\Session;
use think\Request;

class User extends Common
{
    /**
     * 检查是否登陆
     */
    public function __construct(){
        parent::__construct();
        $userid = Session::get('id');
        if(empty($userid)){
            $this->redirect('Login/login');
        }
    }

	/**
	 * 用户中心
	 */
	public function user_center(){	
        $data = $this->headerData();
        $this->assign('data',$data);
		return $this->fetch();
	}


    /**
     * 用户信息
     */
    public function user_info(){
    	$data = $this->headerData();
        $userid = Session::get('id');
        $data['login'] = Db('login')->where("id = '$userid'")->find();
        $this->assign('data',$data);
    	return $this->fetch();
    }

    public function order_list(){
    	$data = $this->headerData();
        $this->assign('data',$data);
    	return $this->fetch();
    }

    /**
     * 收货地址列表
     */
    public function address_list(){
    	$data = $this->headerData();
        $id = Session::get('id');
        $address = Db('address')->where('user_id',$id)->select();
        $data['region'] = Db('region')->where('parent_id',0)->select();
        $this->assign('address',$address[0]);
        $this->assign('data',$data);
    	return $this->fetch();
    }

    /**
     * 收货地址添加
     */
    public function address_add(){
        $data = input('post.');
        $data['user_id'] = Session::get('id');
        $data['add_time'] = date('Y-m-d H:i:s');
        Db('address')->insert($data);
        $this->redirect('User/address_list');
    }

    /**
     * 收货地址删除
     */
    public function address_del(){
        $id = input('get.id');
        Db('address')->where('address_id',$id)->delete();
        $this->redirect('User/address_list');
    }

    /**
     * 收货地址获取
     */
    public function address_info(){
        $id = input('get.parent_id');
        $data = Db('region')->where('parent_id',$id)->select();
        echo json_encode($data);
    }

    /**
     * 收藏添加
     */
    public function collection_list(){
    	$data = $this->headerData();
        $this->assign('data',$data);
    	return $this->fetch();
    }

    /**
     * 留言列表
     */
    public function message_list(){
    	$data = $this->headerData();
        $data['type'] = Db('message_type')->select();
        $data['message'] = Db('message')->select();
        $this->assign('data',$data);
    	return $this->fetch();
    }

    /**
     * 留言添加
     */
    public function message_add(){
        $data = input('post.');
        $file = request()->file("msg_img");
        $data['msg_img'] = $this->upload($file);
        $data['user_id'] = Session::get('id');
        $data['add_time'] = time();
        Db('message')->insert($data);
        $this->redirect('User/message_list');
    }

    /**
     * 留言删除
     */
    public function message_del(){
        $id = input('get.id');
        Db('message')->where('message_id',$id)->delete();
        $this->redirect('User/message_list');
    }

    /**
     * 评论列表
     */
    public function comment_list(){
    	$data = $this->headerData();
        $id = Session::get('id');
        $data['comment'] = Db('comment')->alias('c')->join('goods g','c.goods_id = g.goods_id')->where('user_id',$id)->select();
        $this->assign('data',$data);
    	return $this->fetch();
    }

    /**
     * 评论删除
     */
    public function comment_del(){
        $id = input('get.id');
        Db('comment')->where('comment_id',$id)->delete();
        $this->redirect('User/comment_list');
    }

    public function account_list(){
    	$data = $this->headerData();
        $this->assign('data',$data);
    	return $this->fetch();
    }

    /**
     * 退出登陆
     */
    public function user_out(){
        Session::delete('id');
        Session::delete('username');
        $this->redirect('Login/login');
    }

    /**
     * 上传
     */
    public function upload($file)
    {    
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                 $a=$info->getSaveName();
                 $imgp= str_replace("\\","/",$a);
                 $imgpath='/uploads/'.$imgp;
           
                return $imgpath;
              
            }else{   
                return $file->getError();
            }
        }
    }


}
